
import cloud from '@lafjs/cloud'
import nw from "nw-lafjs";
import {ok} from "@/utils/responseUtil";
// const db = cloud.database()
// const _ = db.command
const mongo = cloud.mongo.db
export default async function (ctx: FunctionContext) {
  let body = ctx.body
  /**
   * {"key":""}
   */
  let whereJson = {deleted:0}
  if (!nw.util.isNull(body.key)){
    // @ts-ignore
    whereJson.name = { $regex: body.key, $options: 'i' }
  }
  let data =  await getDocuments(whereJson)
  return ok(data)

}


const getDocuments = async (whereJson) => {
  return new Promise((resolve, reject) => {
    mongo.collection("region").find(whereJson).toArray((err, docs) => {
      if (err) {
        reject(err);
      } else {
        resolve(docs);
      }
    });
  });
};
